package dog.network;

import java.sql.Timestamp;

import constants.PROTOCOL;
import dog.model.Model;

public class ClientParser
	{
	@SuppressWarnings("unused")
	private Client client;
	private Model model;
	
	public ClientParser(Client client, Model model)
		{
		this.client = client;
		this.model= model;
		}
	public void parse(String parseString)
		{
		System.out.println(parseString);
		String[] pcs = parseString.split("\\|",-1);
		if (pcs.length==0)
			System.out.println("Humbug empty message");
		else 
			{
			Integer type=toInt(pcs[0]);
			if (pcs.length!=PROTOCOL.LEN[type])
				{
				System.out.println("Length mismatch for message type "+type);
				return;
				}
			else
				{
				if(type==PROTOCOL.ROOM)
					{
					model.network_setRoom(toInt(pcs[1]), pcs[2], toInt(pcs[3]));
					System.out.println("Room added or changed. ID: "+pcs[1]);
					}
				else if(type==PROTOCOL.DEPARTMENT)
					{
					model.network_setDepartment(toInt(pcs[1]), pcs[2]);
					System.out.println("Department added or changed");
					}
				else if(type==PROTOCOL.PERSON)
					{
					model.network_setPerson(toInt(pcs[1]), toInt(pcs[5]), pcs[2], pcs[4]);
					System.out.println("Person added or changed");
					}
				else if(type==PROTOCOL.APPOINTMENT)
					{
					model.network_setAppointment(toInt(pcs[1]), toInt(pcs[2]), toTimestamp(pcs[3]), toTimestamp(pcs[4]), pcs[5], pcs[6], toInt(pcs[7]), pcs[8], toInt(pcs[9]), toInt(pcs[10]), toBoolean(pcs[11]));
					System.out.println("Appointment added");
					}
				else if(type==PROTOCOL.MESSAGE)
					{
					model.network_setMessage(toInt(pcs[1]), toInt(pcs[2]), toInt(pcs[3]), toInt(pcs[4]), toInt(pcs[5]), toInt(pcs[6]), pcs[7], toTimestamp(pcs[8]));
					System.out.println("Message received");
					}
				else if(type==PROTOCOL.DEL_ROOM)
					{
					System.out.println("Room deleted");//ILLEGAL
					}
				else if(type==PROTOCOL.DEL_DEPARTMENT)
					{
					System.out.println("Apartment deleted");//ILLEGAL
					}
				else if(type==PROTOCOL.DEL_PERSON)
					{
					System.out.println("Person deleted");//ILLEGAL
					}
				else if(type==PROTOCOL.DEL_APPOINTMENT)
					{
					model.network_cancelAppointment(toInt(pcs[1]),toInt(pcs[2]));
					System.out.println("Appointment cancelled");
					}
				else if(type==PROTOCOL.DEL_MESSAGE)
					{
					System.out.println("You can't ask the client to delete a message");
					}
				else if(type==PROTOCOL.LOGIN_ATTEMPT)
					{
					System.out.println("You cannot log into client");
					}
				else if(type==PROTOCOL.LOGIN_ACCEPT)
					{
					model.network_loginAccept(toInt(pcs[1]),toInt(pcs[2]));
					System.out.println("Login accepted");
					}
				else if(type==PROTOCOL.LOGIN_ERROR)
					{
					model.network_loginFailed();
					System.out.println("Login failed");
					}
				else if(type==PROTOCOL.SET_ATTENDANCE)
					{
					model.network_setAttendance(toInt(pcs[1]),toInt(pcs[2]),toInt(pcs[3]),toInt(pcs[4]));
					System.out.println("Attendance set");
					}
				else if(type==PROTOCOL.ACCESS_RANGE)
					{
					System.out.println("You cannot ask client for appointments");
					}
				else if(type==PROTOCOL.ACCESS_PERSON)
					{
					System.out.println("You cannot ask client for appointments");
					}
				else if(type==PROTOCOL.ACK_APPOINTMENTS)
					{
					model.network_appointmentsDelivered(toInt(pcs[1]));
					System.out.println("Appointments received, type "+pcs[1]);
					}
				else if(type==PROTOCOL.GET_APPOINTMENT)
					{
					System.out.println("You cannot ask client for appointments");
					}
				else if(type==PROTOCOL.UNSET_PERSON)
					{
					System.out.println("Client doesn't handle accesses");
					}
				else if(type==PROTOCOL.UNSET_SPECIFIC)
					{
					System.out.println("Client doesn't handle accesses");
					}
				else if(type==PROTOCOL.POKE)
					{
					System.out.println("Poke!");
					}
				else if(type==PROTOCOL.CHECK_ROOM)
					{
					model.network_roomAvailabilityChecked(toInt(pcs[1]),toBoolean(pcs[2]));
					System.out.println("Room availability received");
					}
				else if(type==PROTOCOL.APPOINTMENT_OK)
					{
					model.network_appointmentOk(toBoolean(pcs[1]));
					System.out.println("Appointment creation response");
					}
				else
					System.out.println("Server cannot handle that humbug command");
				}
			}
		}
	public Integer toInt(String str)
		{
		try 
			{
			return Integer.parseInt(str);
			}
		catch (NumberFormatException e)
			{
			System.out.println("Humbug number format cannot be parsed");
			return null;
			}
		}
	public Long toLong(String str)
		{
		try 
			{
			return Long.parseLong(str);
			}
		catch (NumberFormatException e)
			{
			System.out.println("Humbug number format cannot be parsed");
			return null;
			}
		}
	public Boolean toBoolean(String str)
		{
		if(str.equals("1"))
			return true;
		else
			return false;
		}
	public Timestamp toTimestamp(String str)
		{
		return new Timestamp(toLong(str));
		}
	}
